﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class WorkTime : YJ.Data.Interface.IWorkTime
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public WorkTime()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.WorkTime实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.WorkTime model)
        {
            string sql = @"INSERT INTO WorkTime
				(ID,Year,Date1,Date2,AmTime1,AmTime2,PmTime1,PmTime2) 
				VALUES(:ID,:Year,:Date1,:Date2,:AmTime1,:AmTime2,:PmTime1,:PmTime2)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2, 50){ Value = model.ID },
				new OracleParameter(":Year", OracleDbType.Int32){ Value = model.Year },
				new OracleParameter(":Date1", OracleDbType.Date){ Value = model.Date1 },
				new OracleParameter(":Date2", OracleDbType.Date){ Value = model.Date2 },
				new OracleParameter(":AmTime1", OracleDbType.Varchar2, 50){ Value = model.AmTime1 },
				new OracleParameter(":AmTime2", OracleDbType.Varchar2, 50){ Value = model.AmTime2 },
				new OracleParameter(":PmTime1", OracleDbType.Varchar2, 50){ Value = model.PmTime1 },
				new OracleParameter(":PmTime2", OracleDbType.Varchar2, 50){ Value = model.PmTime2 }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.WorkTime实体类</param>
        public int Update(YJ.Data.Model.WorkTime model)
        {
            string sql = @"UPDATE WorkTime SET 
				Year=:Year,Date1=:Date1,Date2=:Date2,AmTime1=:AmTime1,AmTime2=:AmTime2,PmTime1=:PmTime1,PmTime2=:PmTime2
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":Year", OracleDbType.Varchar2, 50){ Value = model.Year },
				new OracleParameter(":Date1", OracleDbType.Date){ Value = model.Date1 },
				new OracleParameter(":Date2", OracleDbType.Date){ Value = model.Date2 },
				new OracleParameter(":AmTime1", OracleDbType.Varchar2, 50){ Value = model.AmTime1 },
				new OracleParameter(":AmTime2", OracleDbType.Varchar2, 50){ Value = model.AmTime2 },
				new OracleParameter(":PmTime1", OracleDbType.Varchar2, 50){ Value = model.PmTime1 },
				new OracleParameter(":PmTime2", OracleDbType.Varchar2, 50){ Value = model.PmTime2 },
				new OracleParameter(":ID", OracleDbType.Varchar2, 50){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM WorkTime WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.WorkTime> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.WorkTime> List = new List<YJ.Data.Model.WorkTime>();
            YJ.Data.Model.WorkTime model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.WorkTime();
                model.ID = dataReader.GetGuid(0);
                model.Year = dataReader.GetInt32(1);
                model.Date1 = dataReader.GetDateTime(2);
                model.Date2 = dataReader.GetDateTime(3);
                model.AmTime1 = dataReader.GetString(4);
                model.AmTime2 = dataReader.GetString(5);
                model.PmTime1 = dataReader.GetString(6);
                model.PmTime2 = dataReader.GetString(7);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.WorkTime> GetAll()
        {
            string sql = "SELECT * FROM WorkTime";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.WorkTime> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM WorkTime";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.WorkTime Get(Guid id)
        {
            string sql = "SELECT * FROM WorkTime WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.WorkTime> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 查询所有年份
        /// </summary>
        public List<int> GetAllYear()
        {
            string sql = "SELECT DISTINCT Year FROM WorkTime";
            DataTable dt = dbHelper.GetDataTable(sql);
            List<int> list = new List<int>();
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(dr[0].ToString().ToInt());
            }
            return list;
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.WorkTime> GetAll(int year)
        {
            string sql = "SELECT * FROM WorkTime WHERE Year=" + year;
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.WorkTime> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}